package com.twotigers.tenthpower.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.twotigers.tenthpower.user.entity.UserEntity;
import com.twotigers.tenthpower.user.service.UserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 用户
 *
 * @author Erhu
 * @email 1935417243@qq.com
 * @date 2021-03-04 15:14:40
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @GetMapping
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok("查询成功").put("page", page);
    }


    /**
     * 根据id查询信息
     */
    @GetMapping("/{userId}")
    public R info(@PathVariable("userId") String userId){
		UserEntity user = userService.getById(userId);

        return R.ok("查询成功").put("user", user);
    }

    /**
     * 增加用户
     */
    @PostMapping
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok("添加成功");
    }

    /**
     * 修改
     */
    @PutMapping("/{userId}")
    public R update(@RequestBody UserEntity user,@PathVariable("userId")String userId){
        user.setId(userId);
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{userId}")  //删除
    public R delete(@RequestBody String[] userId){
		userService.removeByIds(Arrays.asList(userId));

        return R.ok();
    }

    @PostMapping("/login") // 登录
    public R login(@RequestBody UserEntity userEntity){
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",userEntity.getMobile());
        UserEntity user = userService.getOne(wrapper);
        if(user!=null){
            if(user.getPassword().equals(userEntity.getPassword())){
                return R.ok("登陆成功");
            }else{
                return R.error(500,"密码错误");
            }
        }else{
            return R.error(500,"账号错误");
        }
    }

    //注册用户  发送验证码 未实现

    // 查询登录用户信息   未实现
    @GetMapping("/info")
    public R getLoginUser(){
        return R.ok("查询成功");
    }

}
